package org.lanqiao.lg.servlet;

import static org.lanqiao.tools.Tools.pl;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.handlers.MapListHandler;
import org.lanqiao.bean.Position;
import org.lanqiao.tools.BaseServlet;
import org.lanqiao.wxq.dao.impl.CascadeDaoImpl;
import org.lanqiao.wxq.dao.impl.PositionDaoImpl;

import com.google.gson.Gson;

@WebServlet("/PoServlet")
public class PoServlet extends BaseServlet {
	Gson gson = new Gson();
	PositionDaoImpl podao = new PositionDaoImpl();
	CascadeDaoImpl casdao = new CascadeDaoImpl();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Object id = request.getSession().getAttribute("id");
		Object typeU = request.getSession().getAttribute("typeU");
		this.request = request;
		this.response = response;

		String key = getString("key");
		System.out.println(request.getSession().getId() + key + "/id=" + id + "/typeU=" + typeU);
		redata.setMsg("");
		if (!isNull("key")) {
			switch (key) {
			case "insert":
				inPosition();
				break;
			default:
				redata.setCode(3);
				redata.setMsg("key参数异常");
				redata.setData("");
				break;
			}
		} else {
			redata.setCode(3);
			redata.setMsg("key参数为空");
			redata.setData("");
		}
		System.out.println(redata.toString());
		response.getWriter().write(gson.toJson(redata));
	}

//	添加新的职位
	private void inPosition() {
		Integer companyId = getInt("companyId");
		String positionName = getString("positionName");
		String department = getString("department");
		Integer jobNature = getInt("jobNature");
		Integer salaryMin = getInt("salaryMin");
		Integer salaryMax = getInt("salaryMax");
		String city = getString("city");
		Integer experience = getInt("experience");
		Integer education = getInt("education");
		String attraction = getString("attraction");
		String describe = getString("describe");
		String address = getString("address");
		String email = getString("email");

		
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		Integer casThreeId = casdao.selectCidByName(positionName);
		Integer i = podao.insertPosition(new Position(casThreeId, positionName, department, jobNature, salaryMin,
				salaryMax, city, experience, education, attraction, describe, address, companyId, email));
		if (i > 0) {
			redata.setCode(2);
			redata.setMsg("insertPosition 成功");
			redata.setData(i);
			return;
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData(i);
			return;
		}
	}


}
